//
//  PhotoCViewController.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/5/13.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "PhotoCViewController.h"
#import "Util.h"
#import <UIImageView+AFNetworking.h>
#import "BusinessModel.h"
#import "LNWaterfallFlowLayout.h"
#import "LNWaterfallFlowFooterView.h"
#import "WarEntity.h"
#import "UpPhotoViewC.h"
#import "PhotoDetailViewC.h"

@interface PhotoColCell : UICollectionViewCell
@property (nonatomic, weak) IBOutlet UIImageView *img;
@property (nonatomic, weak) IBOutlet UILabel *textLab;
@property (nonatomic, weak) IBOutlet UIView *backView;
@end

@implementation PhotoColCell
@end

@interface PhotoCViewController (){
    NSArray *dataArraiy;
    NSArray *plistArray;
    NSMutableArray *allArray;
}
// 商品列表数组
@property (nonatomic, strong) NSMutableArray *goodsList;
// 当前的数据索引
@property (nonatomic, assign) NSInteger index;
// 瀑布流布局
@property (weak, nonatomic) IBOutlet LNWaterfallFlowLayout *waterfallFlowLayout;
// 底部视图
@property (nonatomic, weak) LNWaterfallFlowFooterView *footerView;

@end

@implementation PhotoCViewController
//- (void)dealloc{
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.navigationItem.title =@"照片墙";
    allArray = [[NSMutableArray alloc] init];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]
                                 initWithImage:[UIImage imageNamed:@"camera"]
                                 style:UIBarButtonItemStylePlain
                                 target:self
                                 action:@selector(cameraAction:)];
    self.navigationItem.rightBarButtonItem =rightBtn;
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(upPhoto:) name:@"" object:self];
    // Do any additional setup after loading the view.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)upPhoto:(NSNotification *)noti {
//    NSDictionary *dic = [noti object];
//    [allArray addObject:dic];
//    [self.collectionView reloadData];
//}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self getData];
}
- (void)cameraAction:(id)sender{
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                   bundle:nil];
    UpPhotoViewC *upvc = [main instantiateViewControllerWithIdentifier:@"UpPhotoViewC"];
    [self.navigationController pushViewController:upvc animated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)getData{
    
    NSString *gpage =@"1";
    [[BusinessModel sharedInstance] getDatawithpage:gpage data:^(id info) {
        NSArray *array = [info copy];
        dataArraiy = array;
        self.waterfallFlowLayout.columnCount = 2;
        self.waterfallFlowLayout.goodsList = dataArraiy;
        [self.collectionView reloadData];
//        NSString *fileName= [NSString stringWithFormat:
//                             @"%@/Documents/Photo.plist",NSHomeDirectory()];
//        NSMutableArray *phoarray = [NSMutableArray
//                                    arrayWithContentsOfFile:fileName];
//        if (phoarray.count == 0) {
//            
//        }
//        else{
//            plistArray = phoarray;
//        }
    }];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return dataArraiy.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCellID"
                                  forIndexPath:indexPath];
    WarEntity *warEty = dataArraiy[indexPath.row+plistArray.count];
    NSString *str = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",warEty.picPath];
    NSURL *url = [NSURL URLWithString:str];
    cell.textLab.text = [NSString stringWithFormat:@"场馆:%@",warEty.sportsHallName];
    [cell.img setImageWithURL:url];
    [cell bringSubviewToFront:cell.backView];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WarEntity *warEty = dataArraiy[indexPath.row];
    NSString *imagestr = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",warEty.picPath];
    NSString *textstr = [NSString stringWithFormat:@"场馆:%@",warEty.sportsHallName];
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                   bundle:nil];
    PhotoDetailViewC *photovc = [main instantiateViewControllerWithIdentifier:@"PhotoDetailViewC"];
    photovc.imgStr = imagestr;
    photovc.textStr = textstr;
    [self.navigationController pushViewController:photovc animated:YES];
    

}

@end
